exports.refresh = function(player, req, resp, onHandled) {
    var user = player.user;
    
    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }
    } while (false);

    if (resp.code != 0) {
        onHandled && onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        onHandled && onHandled();
    });
}

exports.attack = function(player, req, resp, onHandled) {
   var user = player.user;

    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        var duplicate = user.league_duplicate;
        if (duplicate.attack_times >= (duplicate.buy_times + gConfGlobal.LeagueDuplicateFree)) {
            resp.code = 1; resp.desc = 'no times'; break;
        }

        req.args.inspire = duplicate.inspire;
        req.args.food = user.status.food;
        req.args.level = user.status.level;
    } while (false);

    if (resp.code != 0) {
        onHandled && onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        if (0 == resp.code && !resp.desc) {
            user.league_duplicate.attack_times += 1;
            user.status.food += resp.data.food;
            
            var leagueWar = user.league_war;
            var curGlory = leagueWar.glory || 0;
            var maxGlory = gConfGradeLevel[leagueWar.grade_level].GloryLimit;
            var gloryGot = Math.min(maxGlory-curGlory, resp.data.glory);
            resp.data.glory = gloryGot;
            leagueWar.glory += gloryGot;
            
            player.markDirty('league_duplicate.attack_times');    
            player.markDirty('status.food');
            player.markDirty('league_war.glory');

            var awards = [];
            var duplicateConf = gConfLeagueDuplicate[resp.data.attack_id];
            if (duplicateConf) {
                for (var i = 1; i <= 3; ++i) {
                    var weight = duplicateConf['Weight'+i];
                    var award = duplicateConf['Award'+i];
                    if (weight && award && common.randValue(10000) < weight) {
                        awards.push(award);
                    }
                }
            }
            resp.data.awards = player.addAwards(awards);
        }

        onHandled && onHandled();
    });
}

exports.buy = function(player, req, resp, onHandled) {
    var user = player.user;

    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        var duplicate = user.league_duplicate;
        var buyConf = gConfBuy[duplicate.buy_times + 1];
        if (!buyConf || !buyConf.LegionDuplicateCost) {
            resp.code = 1; resp.desc = 'no times'; break;
        }

        var costCash = buyConf.LegionDuplicateCost;
        if (!player.costResource('cash', costCash, duplicate.buy_times)) {
            resp.code = 1; resp.desc = 'not enough cash';break;
        }

        duplicate.buy_times += 1;
        player.markDirty('league_duplicate.buy_times');   
        
        resp.data.cash = -costCash;
    } while (false);

    onHandled && onHandled();
}

exports.award = function(player, req, resp, onHandled) {
    var user = player.user;
    
    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }
    } while (false);

    if (resp.code != 0) {
        onHandled && onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        if (0 == resp.code) {
            var awardType = resp.data.type;
            var chapterId = +req.args.id;
            var awardConf = gConfAvLeagueDuplicate[chapterId][awardType];
            if (awardConf) {
                var awards = [awardConf.Award1, awardConf.Award2, awardConf.Award3, awardConf.Award4];
                resp.data.awards = player.addAwards(awards);
            }
        }
        onHandled && onHandled();
    });
}

exports.reset = function(player, req, resp, onHandled) {
    var user = player.user;
    
    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        req.args.cash = user.status.cash;
    } while (false);

    if (resp.code != 0) {
        onHandled && onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        if (0 == resp.code) {
            if (resp.data.cash) {
                var costCash = resp.data.cash;
                player.costResource('cash', costCash);
            }
        }
        onHandled && onHandled();
    });
}

exports.inspire = function(player, req, resp, onHandled) {
    var user = player.user;
    
    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (isNaN(req.args.type)) {
            resp.code = 1; resp.desc = 'invalid arg type'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }

        var duplicate = user.league_duplicate;
        var type = +req.args.type;
        var costHonor = 0;
        var costCash = 0;

        if (duplicate.inspire >= gConfGlobal.LeagueDuplicateInspireLimit) {
            resp.code = 1; resp.desc = 'inspire full'; break;
        }

        if (1 == type) {
            costCash = gConfGlobal.LeagueDuplicateInspireCash;
            if (!player.costResource('cash', costCash, duplicate.inspire)) {
                resp.code = 1; resp.desc = 'not enough cash';break;
            }
        } else {
            costHonor = gConfGlobal.LeagueDuplicateInspireHonor;
            if (!player.costResource('honor', costHonor, duplicate.inspire)) {
                resp.code = 1; resp.desc = 'not enough honor';break;
            }
        }

        duplicate.inspire++;
        player.markDirty('league_duplicate.inspire');

        resp.data.cash = -costCash;
        resp.data.honor = -costHonor;
    } while (false);

    onHandled && onHandled();
}

exports.get_rank = function(player, req, resp, onHandled) {
    var user = player.user;
    
    do {
        if (!user.league.name) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if (!gConfSwitch.LeagueDuplicate) {
            resp.code = 1; resp.desc = 'not open'; break;
        }
    } while (false);

    if (resp.code != 0) {
        onHandled && onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        onHandled && onHandled();
    });
}
